import {
  getThemeInfo,
  getThemeStatistics,
  getThemeSpreadEffect,
  getThemeSite_top,
  getThemeAccount_top,
  getThemeSpreadTrend,
  getThemeHotReportArea,
} from '@/api/manageEnd/theme/themeEffectAssess'

/**
 * @desc 主题传播效果指数计算
 * @tips 100×(0.15×ln(E2+1) + 0.2×ln(F2+1) + 0.2×ln(G2+1) + 0.15×ln(H2+1) + 0.15×ln(I2+1) + 0.15×ln(J2+1))
      其中E2是信息量，F2是阅读量，G2是转发量，H2是点赞量 ，I2是评论量，J2是播放量 
*/
const calculateSpreadIndex = (data, suffix = '') => {
  if (typeof data !== 'object' || data === null) return 0
  const {
    ['count' + suffix]: E2,
    ['read' + suffix]: F2,
    ['forward' + suffix]: G2,
    ['like' + suffix]: H2,
    ['comment' + suffix]: I2,
    ['play' + suffix]: J2,
  } = data

  const _index =
    0.15 * Math.log(E2 + 1) +
    0.2 * Math.log(F2 + 1) +
    0.2 * Math.log(G2 + 1) +
    0.15 * Math.log(H2 + 1) +
    0.15 * Math.log(I2 + 1) +
    0.15 * Math.log(J2 + 1)
  return (_index * 100).toFixed()
}

export default {
  methods: {
    /* 获取详情 */
    async getThemeInfo(id) {
      const res = await getThemeInfo(id)
      this.manuInfo = res.data.data || {}
    },

    /* 获取数量统计数据 */
    async getThemeStatistics(id) {
      const res = await getThemeStatistics(id)
      // console.log('getThemeStatistics', res)
      if (res.data.status !== 0) return
      const data = res.data.data || {}
      this.entryDataCountStat.forEach((it) => (it.num = data[it.field] || 0))
      this.allPlatformDataCountStat.forEach((it) => (it.num = data[it.field + 'All'] || 0))
    },

    /* 主题传播效果指数 */
    async getThemeSpreadEffect(id, params) {
      const res = await getThemeSpreadEffect(id, params)
      // console.log('getThemeSpreadEffect', res)
      if (res.data.status !== 0) return (this.show.spreadEffectIndex = false)
      const data = res.data.data || []
      if (!data.length) {
        this.show.spreadEffectIndex = false
        return
      }

      this.show.spreadEffectIndex = true
      const chartData = {
        xData: [],
        allPlatformData: [],
        entryData: [],
      }
      for (let i = 0, len = data.length; i < len; i++) {
        const { key, value } = data[i]
        chartData.xData.push(key)
        chartData.allPlatformData.push(calculateSpreadIndex(value, 'All'))
        chartData.entryData.push(calculateSpreadIndex(value))
      }
      await this.$nextTick()
      this.spreadEffectIndex_chart(chartData)
    },

    /* 平台贡献率top5 */
    async getThemeSite_top(id, params) {
      const res = await getThemeSite_top(id, params)
      // console.log('getThemeSite_top', res)
      if (res.data.status !== 0) return (this.show.platformContribute = false)
      const data = res.data.data || []
      if (!data.length) {
        this.show.platformContribute = false
        return
      }

      this.show.platformContribute = true
      const chartData = data.map((it) => ({
        name: it.key,
        value: it.value,
      }))
      await this.$nextTick()
      this.platformContribute_chart({ data: chartData })
    },

    /* 账号贡献率top5 */
    async getThemeAccount_top(id, params) {
      const res = await getThemeAccount_top(id, params)
      // console.log('getThemeAccount_top', res)
      if (res.data.status !== 0) return (this.show.accountContribute = false)
      const data = res.data.data || []
      if (!data.length) {
        this.show.accountContribute = false
        return
      }

      this.show.accountContribute = true
      const chartData = data.map((it) => ({
        name: it.key,
        value: it.value,
      }))
      await this.$nextTick()
      this.accountContribute_chart({
        data: chartData,
      })
    },

    /* 传播趋势 */
    async getThemeSpreadTrend(id, params) {
      const res = await getThemeSpreadTrend(id, params)
      // console.log('getThemeSpreadTrend', res)
      if (res.data.status !== 0) return (this.show.spreadTrend = false)
      const data = res.data.data || []
      if (!data.length) {
        this.show.spreadTrend = false
        return
      }

      this.show.spreadTrend = true
      const chartData = {
        xData: [],
        reshipData: [],
        readData: [],
      }
      data.forEach((it) => {
        chartData.xData.push(it.key)
        chartData.reshipData.push(it.value.forwardAll || 0)
        chartData.readData.push(it.value.readAll || 0)
      })
      await this.$nextTick()
      this.spreadTrend_chart(chartData)
    },

    /* 热点报道地域分布  */
    async getThemeHotReportArea(id, params) {
      const res = await getThemeHotReportArea(id, params)
      if (res.data.status !== 0) {
        this.show.hotReportArea = false
        this.hotReportArea.allData = 0
        this.hotReportArea_setTableData()
        return
      }
      const data = res.data.data || []
      if (!data.length) {
        this.show.hotReportArea = false
        this.hotReportArea.allData = []
        this.hotReportArea_setTableData()
        return
      }

      this.show.hotReportArea = true
      const mapData = []
      let maxValue = 0
      let totalCount = 0
      data.forEach((it) => {
        mapData.push({ name: it.key, value: it.value })
        maxValue = Math.max(maxValue, Number(it.value))
        totalCount += Number(it.value)
      })
      maxValue = maxValue + Math.ceil(maxValue * 0.1)

      await this.$nextTick()
      this.hotReportArea_chart({ data: mapData, maxValue })
      this.hotReportArea.allData = Object.freeze(data)
      this.hotReportArea.page.total = data.length
      this.hotReportArea.totalCount = totalCount
      this.hotReportArea_setTableData()
    },
  },
}
